import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class binning {

	public static void main(String[] args) {
		String input = args[0];
		int bin_size = Integer.parseInt(args[1]);
		List<bin> b = new ArrayList<bin>();
		readfile(input, b, bin_size);
		writefile(b);
	}
	static void writefile(List<bin> bin) {
		for(int i = 0 ; i < bin.size() ; i++) {
			System.out.println(bin.get(i).getQry_id() + "\t" + bin.get(i).getQry_start() + "\t" + bin.get(i).getQry_end() +
					"\t" + bin.get(i).getRef_id() + "\t" + bin.get(i).getRef_start()  + "\t" + bin.get(i).getRef_end() +
					"\t" + bin.get(i).getStrand());
		}
	}
	
	static void readfile(String input, List<bin> b, int bs) {
		try(BufferedReader br = new BufferedReader(new FileReader(input))){
			String line;
			String id = "";
			String strand = "";
			String qry_id = "";
			String ref_id = "";
			String prev_qid = "";
			List<Integer> qc = new ArrayList<Integer>();
			List<Integer> rc = new ArrayList<Integer>();
			while((line = br.readLine()) != null) {
				String[] strs = line.split("\t");
				int q_coor = Integer.parseInt(strs[1]);
				int r_coor = Integer.parseInt(strs[3]);
				qry_id = strs[0];
				ref_id = strs[2];
				if(id.equals(strs[5])) {
					qc.add(q_coor);
					rc.add(r_coor);
				}else {
					if(qc.size() != 0) {
						int[] qry_coor = new int[qc.size()];
						int[] ref_coor = new int[rc.size()];
						for(int i = 0 ; i < qry_coor.length ; i++) {
							qry_coor[i] = qc.get(i);
						}
						for(int i = 0 ; i < ref_coor.length ; i++) {
							ref_coor[i] = rc.get(i);
						}
						qc.clear();
						rc.clear();
						b.addAll(binning(prev_qid, ref_id, qry_coor, ref_coor ,strand, bs));
					}
					id = strs[5];
					prev_qid = qry_id;
					strand = strs[4];
					qc.add(q_coor);
					rc.add(r_coor);
				}
			}
			int[] qry_coor = new int[qc.size()];
			int[] ref_coor = new int[rc.size()];
			for(int i = 0 ; i < qry_coor.length ; i++) {
				qry_coor[i] = qc.get(i);
			}
			for(int i = 0 ; i < ref_coor.length ; i++) {
				ref_coor[i] = rc.get(i);
			}
			b.addAll(binning(prev_qid, ref_id, qry_coor, ref_coor, strand, bs));
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public static List<bin> binning(String qry_id, String ref_id, int[] qry_coor, int[] ref_coor, String strand, int bs) {
		int bin_size = bin_size(bs);
		int coor_num = qry_coor.length;
		List<bin> bin_list = new ArrayList<bin>();
		
		int rec_qs = qry_coor[0];
		int rec_qe = qry_coor[1];
		int rec_rs = ref_coor[0];
		int rec_re = ref_coor[1];
		for(int i = 0 ; i < coor_num - 1 ; i++) {
			if(strand.equals("+")) {
				if(rec_qe - rec_qs < bin_size && rec_re - rec_rs < bin_size) {
					rec_qe = qry_coor[i + 1];
					rec_re = ref_coor[i + 1];
				}else {
					bin b = new bin(qry_id, ref_id, rec_qs, rec_qe, rec_rs, rec_re, strand);
					bin_list.add(b);
					rec_qs = rec_qe;
					rec_rs = rec_re;
				}
			}else {
				if(rec_qs - rec_qe < bin_size && rec_re - rec_rs < bin_size) {
					rec_qe = qry_coor[i + 1];
					rec_re = ref_coor[i + 1];
				}else {
					bin b = new bin(qry_id, ref_id, rec_qs, rec_qe, rec_rs, rec_re, strand);
					bin_list.add(b);
					rec_qs = rec_qe;
					rec_rs = rec_re;
				}
			}
		}
		if(rec_qs != rec_qe && rec_rs != rec_re) {
			bin b = new bin(qry_id, ref_id, rec_qs, rec_qe, rec_rs, rec_re, strand);
			bin_list.add(b);
		}
		return bin_list;
	}
	
	public static int bin_size(int bin) {
		int bin_size = bin;
		
		
		return bin_size;
	}

}

class bin{
	String qry_id = "";
	String ref_id = "";
	int qry_start = 0;
	int qry_end  = 0;
	int ref_start = 0;
	int ref_end = 0;
	String strand;
	
	public bin(String qi, String ri, int qs, int qe, int rs, int re, String s) {
		qry_id = qi;
		ref_id = ri;
		qry_start = qs;
		qry_end = qe;
		ref_start = rs;
		ref_end = re;
		strand = s;
	}

	public int getQry_start() {
		return qry_start;
	}

	public int getQry_end() {
		return qry_end;
	}

	public int getRef_start() {
		return ref_start;
	}

	public int getRef_end() {
		return ref_end;
	}

	public String getQry_id() {
		return qry_id;
	}

	public String getRef_id() {
		return ref_id;
	}

	public String getStrand() {
		return strand;
	}
	
}






